

import 'package:erp/DataInterface/HttpInterfaces.dart';
import 'package:erp/UI/Components/MessageDialog.dart';
import 'package:erp/global.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ServerSettingsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => ServerSettingsPageState();
}

const SERVER_IP_KEY = "server.ip";
const SERVER_TIMEOUT_KEY = "server.timeout";

class ServerSettingsPageState extends State<ServerSettingsPage> {

  TextEditingController ipController;
  TextEditingController timeOutController;

  ServerSettingsPageState(){
    ipController = TextEditingController(text: globalPreferences.get(SERVER_IP_KEY));
    timeOutController = TextEditingController(text: globalPreferences.get(SERVER_TIMEOUT_KEY).toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Server configuration"),
        centerTitle: true,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      body: Padding(
        padding: EdgeInsets.all(10.0),
        child: ListView(
          children: <Widget>[
            Card(
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        SizedBox(
                            child: Text("Server address",),
                            width: 100.0
                        ),
                        SizedBox(width: 20.0,),
                        Expanded(child: TextField(
                          controller: ipController,
                          decoration: InputDecoration(
                            labelText: "IP or http address",
                            hintText: "http://116.62.214.116:8812"
                          ),
                        ),)
                      ],
                    ),
                    Divider(),
                    Row(
                      children: <Widget>[
                        SizedBox(
                            child: Text("Connection timeout",),
                            width: 100.0
                        ),
                        SizedBox(width: 20.0,),
                        Expanded(child: TextField(
                          controller: timeOutController,
                          decoration: InputDecoration(
                              labelText: "timeout (Milliseconds)",
                              hintText: "For example: 5000"
                          ),
                          keyboardType: TextInputType.numberWithOptions(signed: true, decimal: false),
                        ),)
                      ],
                    ),
                  ],
                ),
              )
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.save),
          onPressed: () async {
            if (ipController.text.isEmpty || timeOutController.text.isEmpty){
              showMessageDialog(context, title: "ئەسكەرتىش", content: "مۇلازىمىتىر تەڭشەكلىرىنى تولۇق كىرگۈزۈڭ");
              return;
            }
            try {
              globalPreferences.setString(SERVER_IP_KEY, ipController.text);
              globalPreferences.setInt(SERVER_TIMEOUT_KEY, int.parse(timeOutController.text));
            }
            catch (exception){
              showMessageDialog(context, title: "ئەسكەرتىش", content: "مۇلازىمىتىر تەڭشەكلىرىدە خاتالىق مەۋجۇت، تۈزىتىپ قايتا سىناپ ىقىڭ");
              return;
            }

            externalInterface = HttpInterfaces();

            Navigator.of(context).pop();
          }
      ),
    );
  }
}